/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-03-24
 * Time: 13:35
 */

class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        //1.创建dp表
        int m = dungeon.length,n=dungeon[0].length;
        int[][] dp = new int[m+1][n+1];
        //2.初始化
        for(int i = m; i >= 0; i--) {
            dp[i][n] = Integer.MAX_VALUE;
        }
        for(int j = n; j >= 0; j--) {
            dp[m][j] = Integer.MAX_VALUE;
        }
        dp[m][n-1] = dp[m-1][n] = 1;
        //3.状态转移
        for(int i = m-1; i >= 0; i--) {
            for(int j = n-1; j >= 0; j--) {
                dp[i][j] = Math.max(1,Math.min(dp[i+1][j],dp[i][j+1])-dungeon[i][j]);
            }
        }
        //4.遍历顺序从右往左从下往上
        //5.返回值
        return dp[0][0];
    }
}

